﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpiceNet.Integration;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;
using SpiceNet.Diagnostics;
using SpiceNet.Simulations;

namespace SpiceNet.Circuits
{
    public partial class Circuit
    {
        /// <summary>
        /// Is the current circuit set up?
        /// </summary>
        private bool IsSetup = false;

        /// <summary>
        /// Wrapper for CKTsetup in cktsetup.c
        /// Driver program to iterate through all setup functions
        /// </summary>
        public void Setup()
        {
            if (IsSetup)
                return;
            IsSetup = true;

            // Setup all devices
            NumStates = 0;
            foreach (Type t in models.Keys)
            {
                for (int i = 0; i < models[t].Count; i++)
                    models[t][i].Setup(this);
            }

            // Initialize the current state
            State.Init(this);
            StateComplex.Init(this);
        }

        /// <summary>
        /// Wrapper for CKTunsetup in cktsetup.c
        /// Driver program for unsetupping all devices
        /// </summary>
        public void Unsetup()
        {
            if (!IsSetup)
                return;
            IsSetup = false;

            // Unsetup devices
            foreach (Type t in models.Keys)
            {
                for (int i = 0; i < models[t].Count; i++)
                    models[t][i].Unsetup(this);
            }

            // Destory circuit structures
            State.Destroy();
        }
    }
}
